package com.wskeee.ec.components.skin
{
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.components.ScrollBarBase;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.styles.StyleNames;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class BaseScrollBarSkin extends BaseSkinDrawer
	{
		//上下按钮 icon,滑块icon
		protected var upArrowIcon:Object;	
		protected var downArrowIcon:Object;
		protected var thumbIcon:Object;
		
		public var upArrow:Button;
		public var downArrow:Button;
		public var thumb:Button;
		public var track:Sprite;
		
		//========================= override ====================
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			initPart();
		}
		//========================= end =========================
		
		//========================= park ========================
		protected function initPart():void
		{
			upArrow = new Button();
			downArrow = new Button();
			
			thumb = new Button();
			thumb.label = upArrow.label = downArrow.label = "";
			track = new Sprite();
			
			clip.addChild(track);
			clip.addChild(upArrow);
			clip.addChild(thumb);
			clip.addChild(downArrow);
			
			//---------------------
			//override
			//---------------------
			//set park icon
		}
		
		//-----------------------------------------------------------------
		//  redraw
		//-----------------------------------------------------------------
		override public function redraw():void
		{
			upArrow.upSkin = getSkin("upArrowUpSkin");
			upArrow.overSkin = getSkin("upArrowOverSkin");
			upArrow.downSkin = getSkin("upArrowDownSkin");
			
			downArrow.upSkin = getSkin("downArrowUpSkin");
			downArrow.overSkin = getSkin("downArrowOverSkin");
			downArrow.downSkin = getSkin("downArrowDownSkin");
			
			thumb.upSkin = getSkin("thumbUpSkin");
			thumb.overSkin = getSkin("thumbOverSkin");
			thumb.downSkin = getSkin("thumbDownSkin");
			
			var _thumbIcon:Object = getSkin("thumbIcon");
			
			var trackSkin:Object = getSkin("trackSkin");
			while(track.numChildren>0)track.removeChildAt(0);
			track.graphics.clear();
			if(trackSkin)
			{
				track.addChild(trackSkin as DisplayObject);
				track.scaleX = 1;
				track.scaleY = 1;
			}else
			{
				track.graphics.beginFill(getStyle(StyleNames.BUTTON_DOWN_COLOR) as uint,0.2);
				track.graphics.drawRect(0,0,10,10);
				track.graphics.endFill();
			}
			
			upArrow.icon = (upArrow.upSkin || upArrow.overSkin || upArrow.downSkin) ? null : upArrowIcon;
			downArrow.icon = (downArrow.upSkin || downArrow.overSkin || downArrow.downSkin) ? null : downArrowIcon;
			thumb.icon = _thumbIcon ? _thumbIcon : thumbIcon;
		}
		
		
		//========================= invaidate ===================
		override public function validateProperties():void
		{
			super.validateProperties();
			//计算 上下 控制器
			upArrow[info.wh] = downArrow[info.wh] = bar.hideArrow ? 0 : 16;
			upArrow.visible = downArrow.visible = !bar.hideArrow;
		}
		//========================= end =========================
		
		override public function validateDisplayList():void
		{
			//计算 滑块
			var thumbH:Number = bar.viewarea ? Math.max(12,Math.floor(bar.viewarea[info.wh]/bar.maxScrollPosition*track[info.wh])) : 12;
			if(thumb[info.wh]!=thumbH)
				thumb[info.wh] = Math.min(thumbH,track[info.wh],track[info.wh]);
			thumb.visible = bar.viewarea ? bar.viewarea[info.wh]<bar.maxScrollPosition : true;
			
			//调整位置
			track[info.xy] = upArrow[info.wh];
			thumb[info.xy] = bar.scrollPosition/bar.maxScrollPosition * (track[info.wh] - thumbH) + track[info.xy];
			downArrow[info.xy] = target[info.wh]-downArrow[info.wh];
		}
		//========================= end =========================
		/**
		 * {xy:x/y,wh:width/height,mouseXY:mouseX/mouseY} 
		 * @return 
		 * 
		 */
		protected function get info():Object
		{
			return bar.info;
		}
		protected function get bar():ScrollBarBase
		{
			return target as ScrollBarBase;
		}
		protected function get clip():Sprite
		{
			return ui as Sprite;
		}
	}
}